package com.example.kingdeescan.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.example.kingdeescan.R;
import com.example.kingdeescan.enums.DialogType;
import androidx.annotation.NonNull;

import butterknife.ButterKnife;

/**
 * author  : zly
 * date    : 2020/7/9 17:16
 * desc    : 自定义弹窗
 */
public class CustomDialog extends Dialog{
    private Context context;      // 上下文
    private int layoutResID;      // 布局文件id
    private int[] listenedItems;  // 要监听的控件id

    public DialogType dialogType;

    public CustomDialog(@NonNull Context context, int themeResId, int[] listenedItems, DialogType dialogType) {
        super(context, R.style.dialog_custom);//dialog的样式
        this.context=context;
        this.layoutResID=themeResId;
        this.listenedItems=listenedItems;
        this.dialogType=dialogType;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        setContentView(layoutResID);

        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth()*4/5; // 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);// 点击Dialog外部消失
    }
}
